<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">任务标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">任务类型 (1 command, 2 class, 3 url, 4 eval)</label>
                        <div class="layui-input-block">
                            <div name="type" id="type" value="1" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">任务执行表达式</label>
                        <div class="layui-input-block">
                            <input type="text" name="rule" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">调用任务字符串</label>
                        <div class="layui-input-block">
                            <input type="text" name="target" value="''" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">任务调用参数</label>
                        <div class="layui-input-block">
                            <input type="text" name="parameter" value="NULL" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">已运行次数</label>
                        <div class="layui-input-block">
                            <input type="number" name="running_times" value="0" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">上次运行时间</label>
                        <div class="layui-input-block">
                            <input type="number" name="last_running_time" value="0" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">备注</label>
                        <div class="layui-input-block">
                            <input type="text" name="remark" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">排序，越大越前</label>
                        <div class="layui-input-block">
                            <input type="number" name="sort" value="0" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">任务状态状态[0:禁用;1启用]</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="status" lay-filter="status" lay-skin="switch" lay-text="开启|关闭"/>
                            <input type="text" style="display:none" name="status" value="1" />
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否单次执行 (0 是 1 不是)</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="singleton" lay-filter="singleton" lay-skin="switch" lay-text="重复|单次"/>
                            <input type="text" style="display:none" name="singleton" value="1" />
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            // 相关接口
            const INSERT_API = "/app/admin/th-system-crontab/insert";
            
            // 字段 任务类型 (1 command, 2 class, 3 url, 4 eval) type
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#type").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#type",
                    name: "type",
                    initValue: initValue,
                    data: [{"value":"1","name":"命令行"},{"value":"2","name":"类文件"},{"value":"3","name":"访问网址"},{"value":"4","name":"eval"}],
                    value: "1",
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                })
            });
            
            // 字段 任务状态状态[0:禁用;1启用] status
            layui.use(["form"], function() {
                layui.$("#status").attr("checked", layui.$('input[name="status"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(status)", function(data) {
                    layui.$('input[name="status"]').val(this.checked ? 1 : 0);
                });
            })
            
            // 字段 是否单次执行 (0 是 1 不是) singleton
            layui.use(["form"], function() {
                layui.$("#singleton").attr("checked", layui.$('input[name="singleton"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(singleton)", function(data) {
                    layui.$('input[name="singleton"]').val(this.checked ? 1 : 0);
                });
            })
            
            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (data) {
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>
</html>
